package com.ayoka.parenting.bean;

import android.text.TextUtils;

import com.ayoka.parenting.R;
import com.google.gson.Gson;
import com.jincp.common.utils.SharedPreferenceUtil;
import com.jincp.common.utils.UIUtils;

import java.io.Serializable;

/**
 * Created by jcp on 2018/1/3.
 */

public class UserManager {

    private static UserManager instance;

    public static UserManager getInstance() {
        if (instance == null) {
            synchronized (UserManager.class) {
                if (instance == null) {
                    instance = getUserInformation();
                }
            }
        }
        return instance;
    }

    // 获取用户
    private static UserManager getUserInformation() {
        UserManager user = null;
        String account = SharedPreferenceUtil.getString("USER_INFORMATION", "");
        if (!TextUtils.isEmpty(account)) {
            user = new Gson().fromJson(account, UserManager.class);
        }
        return user;
    }

    // 设置用户
    public static synchronized void setUserInformation(UserManager user) {
        instance = user;
        if (user != null) {
            if (user.user != null) {
                instance.uid = user.user.uid;
            }
            if (user.token != null) {
                instance.auth_token = user.token.auth_token;
                instance.refresh_token = user.token.refresh_token;
            }
        }
        save();
    }

    // 设置用户
    public static void save() {
        if (instance != null) {
            String account = new Gson().toJson(instance);
            SharedPreferenceUtil.putString("USER_INFORMATION", account);
        } else {
            SharedPreferenceUtil.removeSharedKey("USER_INFORMATION");
        }
    }

    public String uid;
    public String auth_token;
    public String refresh_token;

    public UserBean user;
    public TokenBean token;

    public UserInfoDetailBean detailBean;

    public String tim_user_sig;
    public String tim_sig_expire;
    public int tim_sdk_appid;

    public static String getMemberType(int membership) {
        switch (membership){
            case 0:
                return UIUtils.getString(R.string.normal_member);
            case 1:
                return UIUtils.getString(R.string.temp_member);
            case 2:
                return UIUtils.getString(R.string.haba_member);
            default:
                return "";
        }
    }

    public static String getCoachType(int isCoach) {
        return UIUtils.getString(R.string.haba_coach);
    }


    public static class UserBean implements Serializable {

        public String uid;
        public int completedInfo;
        public int bindCellphone;
        public int userStatus;
    }

    public static class TokenBean implements Serializable {

        public String auth_token;
        public String refresh_token;
        public String token_type;
        public int expires_in;
    }
}
